//
//  ViewController.m
//  YJTableViewCell
//
//  Created by yuanjiang on 2016/12/15.
//  Copyright © 2016年 YuanJiang. All rights reserved.
//

#import "ViewController.h"
#import "YJtableviewcell.h"
#import "HeaderViewCell.h"
#import "YJCellNumberControlller.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

/**tableView*/
@property(nonatomic,strong) UITableView *sectionTableView;

/**section数组*/
@property(nonatomic,strong) NSMutableArray *sectionArray;

//存放头视图展开状态的字典
@property(nonatomic,strong) NSMutableDictionary  *sectionIsShowInAll;

//存放头视图的字典
@property(nonatomic,strong) NSMutableDictionary  *sectionHeaderView;

@end

@implementation ViewController

-(NSMutableDictionary *)sectionIsShowInAll
{
    if (!_sectionIsShowInAll) {
        self.sectionIsShowInAll = [NSMutableDictionary dictionary];
    }
    return _sectionIsShowInAll;
}

-(NSMutableDictionary *)sectionHeaderView
{
    if (!_sectionHeaderView) {
        self.sectionHeaderView = [NSMutableDictionary dictionary];
    }
    return _sectionHeaderView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self dataSource];
    [self  sectionTableView];
}

-(void)dataSource
{
    self.sectionArray = [NSMutableArray array];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"OrderList.plist" ofType:nil
    ];
    NSDictionary *dic =[NSDictionary dictionaryWithContentsOfFile:path];
    _sectionArray = [dic objectForKey:@"orderArray"];
    
}

-(UITableView *)sectionTableView
{
    if (!_sectionTableView) {
        self.sectionTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        self.sectionTableView.delegate= self;
        self.sectionTableView.dataSource =self;
        [self.sectionTableView registerClass:[YJtableviewcell class] forCellReuseIdentifier:@"yjcell"];
        self.sectionTableView.tableFooterView = [[UIView alloc] init];
        self.sectionTableView.sectionFooterHeight=0.0000001;
        [self.view addSubview:self.sectionTableView];
    }
    return _sectionTableView;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_sectionArray count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *rowArr = [[_sectionArray objectAtIndex:section] objectForKey:@"array"];
    //先判断是否存放过该section分组对应的展开状态,若么有存过先默认为收起状态
    
    if ([self.sectionIsShowInAll objectForKey:[NSString stringWithFormat:@"%ld",section]] == nil) {
        [self.sectionIsShowInAll setObject:[NSNumber numberWithBool:NO] forKey:[NSString stringWithFormat:@"%ld",section]];
    }
    
    return [YJCellNumberControlller cellNumberWith:rowArr andIsShow:[[self.sectionIsShowInAll objectForKey:[NSString stringWithFormat:@"%ld",section]] boolValue]];
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HeaderViewCell *headerView = [self.sectionHeaderView objectForKey:[NSString stringWithFormat:@"%lu",section]];
    if (headerView == nil) {
        headerView = [[HeaderViewCell alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20)];
        [headerView setCellNumber:_sectionArray section:section];
        [self.sectionHeaderView setObject:headerView forKey:[NSString stringWithFormat:@"%lu", section]];
    }

    NSDictionary *dic = [_sectionArray objectAtIndex:section];
    headerView.textlabel.text = [dic objectForKey:@"orderId"];
    headerView.statusLabel.text = [dic objectForKey:@"title"];

    __weak ViewController *weakself = self;
    headerView.yjblock = ^(NSDictionary *dic){
        [weakself.sectionIsShowInAll setObject:[dic objectForKey:@"isShow"] forKey:[NSString stringWithFormat:@"%@",[dic objectForKey:@"section"]]];
            [_sectionTableView reloadData];
    };
    
    return headerView;
                                     
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    YJtableviewcell *cell = [tableView dequeueReusableCellWithIdentifier:@"yjcell"];
    cell.selectionStyle=NO;
    
    NSArray *rowArr = [[_sectionArray objectAtIndex:indexPath.section] objectForKey:@"array"];
    
    NSDictionary *cellDic =[rowArr objectAtIndex:indexPath.row];
    
    cell.nameLabel.text = [cellDic objectForKey:@"name"];
    
    cell.numLabel.text = [NSString stringWithFormat:@"x%@",[cellDic objectForKey:@"num"]];

    cell.moneyLabel.text =[NSString stringWithFormat:@"￥%@",[cellDic objectForKey:@"sale"]];
    
    return cell;
}
















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
